package com.zksk.portal.controller.finance;

import com.zksk.admin.api.RemoteProductService;
import com.zksk.common.core.constant.OrgConstants;
import com.zksk.common.core.constant.SecurityConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.*;
import com.zksk.portal.domain.vo.finance.LGApplyForBasis;
import com.zksk.portal.domain.vo.finance.LGApplyForBasisBuild;
import com.zksk.portal.service.member.MemberService;
import com.zksk.portal.service.section.SectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 保函申请
 */
@Slf4j
@RestController
@RequestMapping("/applyFor")
@Api(value = "LGApplyForController", tags = "门户金融服务相关接口")
public class LGApplyForController extends BaseController {


    @Autowired
    private MemberService memberService;

    @Autowired
    private SectionService sectionService;

    @Autowired
    private RemoteProductService remoteProductService;

    /**
     * 接口：获取页面显示数据
     * @return
     */
    @Log(title = "获取页面显示数据", businessType = BusinessType.SELECT)
    @ApiOperation(value = "获取页面显示数据")
    @GetMapping("/{customerId}/build")
    public R<LGApplyForBasisBuild> lgApplyForBasis(@PathVariable("customerId") Long customerId,
                                                   @RequestParam("sectionId") Long sectionId,
                                                   @RequestParam("memberId") Long memberId){
        LGApplyForBasisBuild basisBuild = new LGApplyForBasisBuild();
        // 获取企业信息
        Member member = memberService.getById(memberId);
        basisBuild.setMember(member);
        // 获取标段信息
        Section section = sectionService.getById(sectionId);
        basisBuild.setSection(section);

        // 获取客户关联得机构 及其 机构产品
        R<List<ProductRelease>> listByCustomerId = remoteProductService.getListByCustomerId(customerId, SecurityConstants.INNER);
        List<ProductRelease> releaseList = listByCustomerId.getData(); // 关联产品列表
        // 根据发布得产品 获取产品对应的机构及其机构产品
        basisBuild.setReleaseList(releaseList);

        // 填充不同类型
        basisBuild.setYinhang(releaseList.stream().filter(item ->
                item.getOrgType().equals(OrgConstants.ORG_TYPE_YH)).collect(Collectors.toList()));

        basisBuild.setBaoxian(releaseList.stream().filter(item ->
                item.getOrgType().equals(OrgConstants.ORG_TYPE_BX)).collect(Collectors.toList()));

        basisBuild.setDanbao(releaseList.stream().filter(item ->
                item.getOrgType().equals(OrgConstants.ORG_TYPE_DB)).collect(Collectors.toList()));

        basisBuild.setQita(releaseList.stream().filter(item ->
                item.getOrgType().equals(OrgConstants.ORG_TYPE_QT)).collect(Collectors.toList()));

        // 组装其他信息 - 机构及其机构产品列表 字段等信息
        basisBuild.setBasisList(
            releaseList.stream().map(this::buildLGApplyForBasis).collect(Collectors.toList())
        );
        return R.ok(basisBuild, "获取数据成功");
    }

    /**
     * 根据关联产品组装
     * @param release
     * @return
     */
    private LGApplyForBasis buildLGApplyForBasis(ProductRelease release){
        LGApplyForBasis lgApplyForBasis = new LGApplyForBasis();
        lgApplyForBasis.setRelease(release);

        // 获取机构信息
        Long orgId = release.getOrgId();
        Org org = remoteProductService.getOrgById(orgId, SecurityConstants.INNER).getData();
        lgApplyForBasis.setOrg(org);

        // 获取对应得机构产品列表
        Long orgProductId = release.getOrgProductId();
        OrgProduct orgProduct = remoteProductService.getOrgProduct(orgProductId, SecurityConstants.INNER).getData();
        lgApplyForBasis.setOrgProduct(orgProduct);

        // 获取对应的字段
        List<FieldSelect> fieldSelects = remoteProductService.getListByOrgProductId(orgProductId, SecurityConstants.INNER).getData();

        // 标段
        List<FieldSelect> sectionFieldsCollect = Optional.ofNullable(fieldSelects)
                .orElseGet(Collections::emptyList)
                .stream()
                .filter(p -> p.getFieldType().equals(OrgConstants.ORG_TABLE_TYPE_SECTION))
                .collect(Collectors.toList());

        lgApplyForBasis.setSectionFields(sectionFieldsCollect);

        // 企业信息
        List<FieldSelect> companyFieldsCollect = Optional.ofNullable(fieldSelects)
                .orElseGet(Collections::emptyList)
                .stream()
                .filter(p -> p.getFieldType().equals(OrgConstants.ORG_TABLE_TYPE_COMPANY))
                .collect(Collectors.toList());

        lgApplyForBasis.setCompanyFields(companyFieldsCollect);

        // 获取协议 - 机构
        List<Agreement> orgAgreements = remoteProductService.getOrgAgreements(orgProductId, SecurityConstants.INNER).getData();
        lgApplyForBasis.setOrgAgreements(Optional.ofNullable(orgAgreements)
                .orElseGet(Collections::emptyList));

        // 获取协议 - 客户
        Long customerProdReqId = release.getCustomerProdReqId();
        List<Agreement> customerAgreements = remoteProductService.getCustomerAgreements(customerProdReqId, SecurityConstants.INNER).getData();
        lgApplyForBasis.setCustomerAgreements(Optional.ofNullable(customerAgreements)
                .orElseGet(Collections::emptyList));

        return lgApplyForBasis;
    }
}
